package com.oracle.cmp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.protocol.a.LocalDateTimeValueEncoder;
import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.*;
import com.oracle.cmp.service.*;
import com.oracle.jdbc.util.Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/retory.do")
public class PartsRepetoryControlle extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String op = request.getParameter("op");
        if("query".equals(op)){
            query(request,response);
        }else if("querybill".equals(op)){
            querybill(request,response);
        }else if("ajaxquery".equals(op)) {
            ajaxquery(request, response);
        }else if("queryid".equals(op)) {
            queryid(request, response);
        }else if("querycount".equals(op)) {
            querycount(request, response);
        }else if ("save".equals(op)) {
            save(request, response);
        }


    }
    public void query(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsRepoertoryService partsRepoertoryService=new PartsRepetoryImpl();
        String partsName = request.getParameter("partsName");
        String pageNo = request.getParameter("pageNum");
        String partsid=request.getParameter("partsId");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("partsName",partsName);
        map.put("partsId",partsid);
        Result result =partsRepoertoryService.queryForPage(map,pageNum);
        int total = partsRepoertoryService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("partsName",partsName);
        request.setAttribute("partsId",partsid);
        request.getRequestDispatcher("/page/partssys/partsrep/partsreplist.jsp").forward(request,response);
    }
    public void querybill(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PartsRepetoryBillService partsRepetoryBillService =  new PartsRepetoryBillImpl();
        String partsName = request.getParameter("partsName");
        String pageNo = request.getParameter("pageNum");
        String billflag=request.getParameter("billflag");
        String billtype=request.getParameter("billtype");
        String billtime=request.getParameter("billtime");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("partsName",partsName);
        map.put("billflag",billflag);
        map.put("billtype",billtype);
        map.put("billtime",billtime);
        Result result =partsRepetoryBillService.queryForPage(map,pageNum);
        int total = partsRepetoryBillService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("partsName",partsName);
        request.setAttribute("billflag",billflag);
        request.setAttribute("billtype",billtype);
        request.setAttribute("billtime",billtime);
        request.getRequestDispatcher("/page/partssys/partsrepbill/partsrepbilllist.jsp").forward(request,response);
    }
    private void ajaxquery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String billflag=req.getParameter("billflag");
        String sql="SELECT code,name FROM sys_code WHERE type=?";
        List<Code> codes= Dao.query(sql,Code.class,billflag);
        resp.getWriter().print(new ObjectMapper().writeValueAsString(codes));
    }

    private void queryid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql="select PartsId,PartsName FROM biz_parts";
        List<Parts> parts= Dao.query(sql,Parts.class);
        resp.getWriter().print(new ObjectMapper().writeValueAsString(parts));
    }
    private void querycount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String partsid=req.getParameter("partsid");
        PartsRepertory partsRepertory=new PartsRepetoryImpl().selectById(Integer.parseInt(partsid));
        resp.getWriter().print(partsRepertory.getPartsreqcount());
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Partsrepbill partsrepbill=new Partsrepbill();
        partsrepbill.setBillflag(req.getParameter("billflag"));
        partsrepbill.setBilltype(req.getParameter("billtype"));
        partsrepbill.setPartsid(Integer.parseInt(req.getParameter("partsid")));
        partsrepbill.setBillcount(Integer.parseInt(req.getParameter("billcount")));
        LocalDateTime time=LocalDateTime.now();
        partsrepbill.setBilltime(time);
        User user= (User)req.getSession().getAttribute("user");
        User loginuser=new LoginService().queryID(user.getLoginname());
        String userid=Integer.toString(loginuser.getUserid());
        partsrepbill.setBilluser(userid);

        PartsRepetoryBillService partsRepetoryBillService=new PartsRepetoryBillImpl();
        Result result=partsRepetoryBillService.save(partsrepbill);
        PartsRepertory partsRepertory=new PartsRepetoryImpl().selectById(Integer.parseInt(req.getParameter("partsid")));
        result.setData(partsRepertory);
        resp.getWriter().print(new ObjectMapper().writeValueAsString(result));

    }
}

